﻿Public Class TCPInfo
    Implements ICloneable

    Public Sub New()
        ReDim mMAC(5), mIP(3), mIPMask(3), mIPGateway(3), mServerIP(99), mDNS(3), mDNSBackup(3)
    End Sub

#Region "MAC"
    Protected mMAC As byte()  '网络MAC码，物理地址

    ''' <summary>
    ''' 网络MAC码，物理地址 00-00-00-00-00-00
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property MAC As String
        Get
            Return ByteToMAC(mMAC)
        End Get
        Set(ByVal value As String)
            mMAC = MACToByte(value)
        End Set
    End Property
#End Region

#Region "IP信息"
    Protected mAutoIP As Boolean '自动获得IP
    Protected mIP As Byte()  'IP地址
    Protected mIPMask As Byte()  'IP掩码
    Protected mIPGateway As Byte()  '网关
    Protected mTCPPort As UInt16  '本地监听端口
    Protected mUDPPort As UInt16  '本地UDP绑定端口

    ''' <summary>
    ''' 自动获得IP
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property AutoIP As Boolean
        Get
            Return mAutoIP
        End Get
        Set(ByVal value As Boolean)
            mAutoIP = value
        End Set
    End Property

    ''' <summary>
    ''' IP地址
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property IP As String
        Get
            Return ByteToIP(mIP)
        End Get
        Set(ByVal value As String)
            mIP = IPToByte(value)
        End Set
    End Property

    ''' <summary>
    ''' IP掩码
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property IPMask As String
        Get
            Return ByteToIP(mIPMask)
        End Get
        Set(ByVal value As String)
            mIPMask = IPToByte(value)
        End Set
    End Property

    ''' <summary>
    ''' 网关
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property IPGateway As String
        Get
            Return ByteToIP(mIPGateway)
        End Get
        Set(ByVal value As String)
            mIPGateway = IPToByte(value)
        End Set
    End Property

    ''' <summary>
    ''' 本地监听端口
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property TCPPort As UInt16
        Get
            Return mTCPPort
        End Get
        Set(ByVal value As UInt16)
            mTCPPort = value
        End Set
    End Property

    ''' <summary>
    ''' 本地UDP绑定端口
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property UDPPort As UInt16
        Get
            Return mUDPPort
        End Get
        Set(ByVal value As UInt16)
            mUDPPort = value
        End Set
    End Property

#End Region

#Region "服务器信息"
    Protected mServerAddrType As Byte '服务器地址类型 1--IP地址（仅用前4字节）； 2--域名（需要DNS解析）；
    Protected mServerProtocolType As Byte '服务器协议类型：1--TCP Server（控制器就是Client）；2--UDP Server；
    Protected mServerIP As Byte()   'TCPServer网络IP或域名
    Protected mServerPort As UInt16  '服务器网络端口
    Protected mDNS As Byte()  'DNS服务器
    Protected mDNSBackup As Byte()  '备用DNS服务器

    ''' <summary>
    ''' 服务器地址类型 
    ''' 1--IP地址（仅用前4字节）； 
    ''' 2--域名（需要DNS解析）；
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ServerAddrType As Byte
        Get
            Return mServerAddrType
        End Get
        Set(ByVal value As Byte)
            mServerAddrType = value
        End Set
    End Property

    ''' <summary>
    ''' 服务器协议类型：
    ''' 1--TCP Server（控制器就是Client）；
    ''' 2--UDP Server；
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ServerProtocolType As Byte
        Get
            Return mServerProtocolType
        End Get
        Set(ByVal value As Byte)
            mServerProtocolType = value
        End Set
    End Property


    ''' <summary>
    ''' 服务器IP
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ServerIP As String
        Get
            Return ByteToIP(mServerIP)
        End Get
        Set(ByVal value As String)
            mServerAddrType = 1
            mServerIP = IPToByte(value)
        End Set
    End Property

    ''' <summary>
    ''' 服务器域名
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ServerAddr As String
        Get
            If mServerIP Is Nothing Then Return String.Empty
            If mServerIP.Length = 0 Then Return String.Empty

            Return BufToStr(mServerIP)
        End Get
        Set(ByVal value As String)
            mServerAddrType = 2
            StrToBuf(value, mServerIP)
        End Set
    End Property

    ''' <summary>
    ''' 服务器端口
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ServerPort As UInt16
        Get
            Return mServerPort
        End Get
        Set(ByVal value As UInt16)
            mServerPort = value
        End Set
    End Property

    ''' <summary>
    ''' DNS服务器
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property DNS As String
        Get
            Return ByteToIP(mDNS)
        End Get
        Set(ByVal value As String)
            mDNS = IPToByte(value)
        End Set
    End Property

    ''' <summary>
    ''' 备用DNS服务器
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property DNSBuckup As String
        Get
            Return ByteToIP(mDNSBackup)
        End Get
        Set(ByVal value As String)
            mDNSBackup = IPToByte(value)
        End Set
    End Property

#End Region

#Region "转换函数"
    Public Shared Function IPToByte(ByVal sIP As String) As Byte()
        Dim sTmp() As String
        Dim bData(3), bNum As Byte
        Dim i As Integer
        If String.IsNullOrEmpty(sIP) Then Return bData

        sTmp = Split(sIP, ".")
        sIP = vbNullString
        For i = 0 To sTmp.Length - 1
            bNum = 0
            Byte.TryParse(sTmp(i), bNum)
            bData(i) = bNum
            If i = 3 Then Exit For
        Next

        Return bData
    End Function

    Public Shared Function ByteToIP(ByVal bData() As Byte) As String
        Dim sIP As String = String.Empty
        Dim i As Byte
        If bData Is Nothing Then Return String.Empty
        If bData.Length = 0 Then Return String.Empty

        For i = 0 To 3
            sIP += bData(i).ToString() + "."
        Next

        sIP = sIP.Substring(0, sIP.Length() - 1)
        Return sIP
    End Function

    ''' <summary>
    ''' 字节数组转MAC
    ''' </summary>
    ''' <param name="bData"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ByteToMAC(ByVal bData() As Byte) As String
        Dim i As Long
        If bData Is Nothing Then Return String.Empty
        If bData.Length = 0 Then Return String.Empty

        Dim sMac As String
        Dim sHex As String = ByteToHex(bData)
        sMac = String.Empty
        For i = 0 To sHex.Length - 1 Step 2
            sMac = sMac & sHex.Substring(i, 2) & "-"
        Next

        If sMac.Length > 0 And Right(sMac, 1) = "-" Then
            sMac = sMac.Substring(0, sMac.Length() - 1)
        End If
        Return sMac
    End Function

    ''' <summary>
    ''' MAC 转换为 字节数组
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function MACToByte(ByVal value As String) As Byte()
        value = value.ToUpper
        value = Replace(value, "-", String.Empty)
        Return HexToByte(CheckHexLen(value, 12))
    End Function
#End Region


    ''' <summary>
    ''' 复制一个TCP信息类
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim oNew As TCPInfo = New TCPInfo()
        With oNew
            If mMAC IsNot Nothing Then .mMAC = mMAC.Clone

            .mAutoIP = mAutoIP
            If mIP IsNot Nothing Then .mIP = mIP.Clone
            If mIPMask IsNot Nothing Then .mIPMask = mIPMask.Clone
            If mIPGateway IsNot Nothing Then .mIPGateway = mIPGateway.Clone
            .mTCPPort = mTCPPort
            .mUDPPort = mUDPPort

            .mServerAddrType = mServerAddrType
            If mServerIP IsNot Nothing Then .mServerIP = mServerIP.Clone
            .mServerPort = mServerPort
            If mDNS IsNot Nothing Then .mDNS = mDNS.Clone
            If mDNSBackup IsNot Nothing Then .mDNSBackup = mDNSBackup.Clone
        End With

        Return oNew
    End Function

#Region "解析TTCPInfo"
    Protected mSetData() As Byte
    Public ReadOnly Property ByteData() As Byte()
        Get
            Return mSetData
        End Get
    End Property

    Public Overridable Sub SetByte(ByVal bData() As Byte)
        mSetData = bData.Clone

        mMAC = copyOfRange(bData, 0, 6) 'MAC地址

        mAutoIP = (bData(6) = 1) '自动获取IP地址(DHCP)
        mIP = copyOfRange(bData, 7, 4) 'IP
        mIPMask = copyOfRange(bData, 11, 4) '子网掩码
        mIPGateway = copyOfRange(bData, 15, 4) '网关IP
        mTCPPort = ByteToInt16(bData, 19)  '本地TCP监听端口
        mUDPPort = ByteToInt16(bData, 21) '本地UDP监听端口

        mServerAddrType = bData(23) '服务器地址类型
        mServerProtocolType = bData(24) '服务器协议类型
        If mServerAddrType = 1 Then 'IP地址
            mServerIP = copyOfRange(bData, 25, 4)
        Else
            mServerIP = copyOfRange(bData, 25, 100)

        End If

        mServerPort = ByteToInt16(bData, 125)  '服务器端口
        mDNS = copyOfRange(bData, 127, 4) '
        If bData.Length < 135 Then Return
        mDNSBackup = copyOfRange(bData, 131, 4)
    End Sub


    Public Sub SetByte_ServerEx(ByVal bData() As Byte)
        mSetData = bData.Clone

        mServerAddrType = bData(0) '服务器地址类型
        mServerProtocolType = bData(1) '服务器协议类型
        If mServerAddrType = 1 Then 'IP地址
            mServerIP = copyOfRange(bData, 2, 4)
        Else
            mServerIP = copyOfRange(bData, 2, 100)

        End If

        mServerPort = ByteToInt16(bData, 102)  '服务器端口
    End Sub
#End Region

    ''' <summary>
    ''' 获取所有IP参数的字节流
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overridable Function GetBytes() As Byte()
        Dim bData(134) As Byte
        Dim bTmpData() As Byte
        Array.Copy(mMAC, 0, bData, 0, 6) 'MAC地址
        bTmpData = GetByte_IPInfo()
        '设置控制器IP信息
        Array.Copy(bTmpData, 0, bData, 6, 17)


        bTmpData = GetByte_ServerInfo()
        '设置控制器的服务器信息
        Array.Copy(bTmpData, 0, bData, 23, 112)

        Return bData
    End Function

    ''' <summary>
    ''' 获取IP地址信息部分的二进制数据
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetByte_IPInfo() As Byte()
        Dim bData(16) As Byte
        If mAutoIP Then bData(0) = 1 '自动获取IP地址(DHCP)
        Array.Copy(mIP, 0, bData, 1, 4) 'IP地址
        Array.Copy(mIPMask, 0, bData, 5, 4) '子网掩码
        Array.Copy(mIPGateway, 0, bData, 9, 4) '网关IP

        Array.Copy(Int16ToByte(mTCPPort), 0, bData, 13, 2) '本地TCP监听端口
        Array.Copy(Int16ToByte(mUDPPort), 0, bData, 15, 2) '本地UDP监听端口
        Return bData
    End Function

    ''' <summary>
    ''' 获取服务器信息部分的二进制数据
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetByte_ServerInfo() As Byte()
        Dim bData(111) As Byte
        bData(0) = mServerAddrType '服务器地址类型
        bData(1) = mServerProtocolType '服务器协议类型
        '服务器地址
        Dim iLen As Integer = mServerIP.Length
        If iLen > 0 Then
            If iLen > 100 Then
                iLen = 100
            End If

            Array.Copy(mServerIP, 0, bData, 2, iLen)
        End If
        Array.Copy(Int16ToByte(mServerPort), 0, bData, 102, 2) '服务器端口

        Array.Copy(mDNS, 0, bData, 104, 4) 'DNS
        Array.Copy(mDNSBackup, 0, bData, 108, 4) '备用DNS

        Return bData
    End Function


    ''' <summary>
    ''' 获取服务器信息部分的二进制数据
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetByte_ServerInfoEx() As Byte()
        Dim bData(103) As Byte
        bData(0) = mServerAddrType '服务器地址类型
        bData(1) = mServerProtocolType '服务器协议类型
        '服务器地址
        Dim iLen As Integer = mServerIP.Length
        If iLen > 0 Then
            If iLen > 100 Then
                iLen = 100
            End If

            Array.Copy(mServerIP, 0, bData, 2, iLen)
        End If
        Array.Copy(Int16ToByte(mServerPort), 0, bData, 102, 2) '服务器端口
        Return bData
    End Function
End Class

Public Class TCPInfo_Door8000
    Inherits TCPInfo

    Private mServerAddr() As Byte
    ''' <summary>
    ''' 服务器域名
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overloads Property ServerAddr As String
        Get
            If mServerAddr Is Nothing Then Return String.Empty
            If mServerAddr.Length = 0 Then Return String.Empty

            Return gEncDefault.GetString(mServerAddr)
        End Get
        Set(ByVal value As String)
            mServerAddr = gEncDefault.GetBytes(value)
        End Set
    End Property


    Public Overrides Sub SetByte(ByVal bData() As Byte)
        mMAC = copyOfRange(bData, 0, 6) 'MAC地址
        mIP = copyOfRange(bData, 6, 4) 'IP
        mIPMask = copyOfRange(bData, 10, 4) '子网掩码
        mIPGateway = copyOfRange(bData, 14, 4) '网关IP
        mDNS = copyOfRange(bData, 18, 4) 'DNS
        mDNSBackup = copyOfRange(bData, 22, 4) '备用DNS
        'bData(26) 'TCP工作模式

        mTCPPort = ByteToInt16(bData, 27)  '本地TCP监听端口
        mUDPPort = ByteToInt16(bData, 29) '本地UDP监听端口

        mServerPort = ByteToInt16(bData, 31)  '服务器端口
        mServerAddrType = 1 'IP地址
        mServerIP = copyOfRange(bData, 33, 4) 'IP地址
        Dim bAddr() As Byte
        '自动获取IP
        mAutoIP = (bData(37) = 1)
        '服务器域名
        bAddr = copyOfRange(bData, 38, 99)
        For i As Integer = 0 To bAddr.Length - 1
            If bAddr(i) = 0 Then
                If i > 0 Then
                    mServerAddr = copyOfRange(bAddr, 0, i)
                Else
                    mServerAddr = Nothing
                    Erase bAddr
                End If
                Exit For
            End If
        Next
    End Sub


    ''' <summary>
    ''' 获取IP地址信息部分的二进制数据
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function GetBytes() As Byte()
        Dim bData(137) As Byte
        Array.Copy(mMAC, 0, bData, 0, 6)  'MAC地址
        Array.Copy(mIP, 0, bData, 6, 4) 'IP地址
        Array.Copy(mIPMask, 0, bData, 10, 4) '子网掩码
        Array.Copy(mIPGateway, 0, bData, 14, 4) '网关IP
        Array.Copy(mDNS, 0, bData, 18, 4) 'DNS
        Array.Copy(mDNSBackup, 0, bData, 22, 4) '备用DNS
        bData(26) = 1 'TCP工作模式
        Array.Copy(Int16ToByte(mTCPPort), 0, bData, 27, 2) '本地TCP监听端口
        Array.Copy(Int16ToByte(mUDPPort), 0, bData, 29, 2) '本地UDP监听端口

        Array.Copy(Int16ToByte(mServerPort), 0, bData, 31, 2) '服务器端口
        Array.Copy(mServerIP, 0, bData, 33, 4) '服务器IP地址
        If mAutoIP Then bData(37) = 1 '自动获取IP

        Dim bLen As Integer = 0
        If mServerAddr Is Nothing Then
            bLen = 0
        Else
            If mServerAddr.Length = 0 Then
                bLen = 0
            Else
                bLen = mServerAddr.Length
            End If
        End If


        If bLen > 0 Then
            Array.Copy(mServerAddr, 0, bData, 38, bLen) '服务器地址
        End If

        Return bData
    End Function
End Class